package admin;

import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;

public class Menu extends Model<Menu> {
	
	private static final long serialVersionUID = 1L;
	public static final Menu dao = new Menu();
	
	
	/**
	 * 展示所有菜单
	 * @return
	 */
	public List<Menu> findAllMenu(){
		return dao.find("select m.id,m.name,m.pid, m.url from menu m  ");
	}
	
	/**
	 * 展示用户权限相应功能菜单
	 * @return
	 */
	public List<Menu> menuListByRoleid(int myroleid){
		return dao.find("select m.id,m.name,m.pid, m.url " +
				"from menu m  where m.id in " +
				"(select menuid from role_menu where roleid = ?) order by m.order_ asc ",myroleid);
	}
	
	/**
	 * 其他用户根据角色获取权限菜单
	 * @return
	 */
	public List<Menu> userFindByRoleid(int myroleid,int roleid){
		return dao.find("select m.id,m.name,m.pid, if(r.id is null,'0','1') as isShow  " +
				"from menu m  left join role_menu r on r.menuid = m.id  and r.roleid = ? where m.id in " +
				"(select menuid from role_menu where roleid = ?)  order by m.order_ asc",roleid,myroleid);
	}
	
	/**
	 * 根据角色获取权限菜单
	 * @return
	 */
	public List<Menu> adminFindByRoleid(int roleid){
		return dao.find("select m.id,m.name,m.pid,if(r.id is null,'0','1') as isShow " +
				"from menu m left join role_menu r on r.menuid = m.id " +
				"  and r.roleid = ?  order by m.order_ asc",roleid);
	}
	
	/**
	 * 获取所有权限菜单
	 * @return
	 */
	public List<Menu> findAll(){
		return dao.find("select m.id,m.name,m.url," +
				"if(mm.name is null,'顶级标题',mm.name) as pname,m.pid " +
				"from menu m left join menu mm on m.pid = mm.id order by m.order_ asc ");
	}
}
